package com.wandian.audio.mvvm.audiosinger;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.R;
import com.wandian.audio.adapter.AudioSingerRecycleAdapter;
import com.wandian.audio.bean.MusicSingerBean;
import com.wandian.audio.databinding.AudioActivitySingerBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 歌手activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_AUDIO_SINGER)
public class AudioSingerActivity extends BaseMVVMActivity<AudioSingerActivityViewModel>  implements  BaseQuickAdapter.RequestLoadMoreListener {
    private AudioActivitySingerBinding dataBinding;
    private AudioSingerRecycleAdapter audioSingerRecycleAdapter;
    private int pageSize =30;
    private int pageNumber =0;
    private String type ="-1";//-1:全部1:男歌手2:女歌手3:乐队
    private String area ="-1";//-1:全部7华语96欧美8:日本16韩国0:其他
    private String singerName = "";
    private List<String> titleList = new ArrayList<>();
    private List<String> areaList = new ArrayList<>();
    private boolean isRefresh = false;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.audio_activity_singer);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initTable();
        initRecycle();
        initViewData();
        initClink();
        getData(true);
    }

    private void getData(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            audioSingerRecycleAdapter.setEnableLoadMore(true);
            pageNumber = 0;
            audioSingerRecycleAdapter.getData().clear();
            audioSingerRecycleAdapter.notifyDataSetChanged();
        }else {
            pageNumber = pageNumber+1;
        }
        mViewModel.getMusicSinger(type,area,singerName,pageSize,pageNumber,System.currentTimeMillis()+"",true);
    }

    private void initTable(){
        //歌手
        titleList.clear();
        titleList.add("全部");
        titleList.add("男歌手");
        titleList.add("女歌手");
        titleList.add("乐队");
        for(int i=0;i<titleList.size();i++){
            dataBinding.tabLayout.addTab(dataBinding.tabLayout.newTab().setText(titleList.get(i)));
        }
        dataBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                pageNumber =0;
                switch (tab.getPosition()){
                    case 0:
                        type = "-1";
                    break;
                    case 1:
                        type = "1";
                        break;
                    case 2:
                        type = "2";
                        break;
                    case 3:
                        type = "3";
                        break;
                }
                getData(true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //区域
        areaList.clear();
        areaList.add("全部");
        areaList.add("华语");
        areaList.add("欧美");
        areaList.add("日本");
        areaList.add("韩国");
        areaList.add("其他");
        for(int i=0;i<areaList.size();i++){
            dataBinding.tabLayoutAre.addTab(dataBinding.tabLayoutAre.newTab().setText(areaList.get(i)));
        }
        dataBinding.tabLayoutAre.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                pageNumber =0;
                switch (tab.getPosition()){
                    case 0:
                        type = "-1";
                        break;
                    case 1:
                        type = "7";
                        break;
                    case 2:
                        type = "96";
                        break;
                    case 3:
                        type = "8";
                        break;
                    case 4:
                        type = "16";
                        break;
                    case 5:
                        type = "0";
                        break;
                }
                getData(true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initRecycle(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.recycler.setLayoutManager(layoutManager);
        audioSingerRecycleAdapter  = new AudioSingerRecycleAdapter(R.layout.audio_item_singer_list_view);
        dataBinding.recycler.setAdapter(audioSingerRecycleAdapter);
        audioSingerRecycleAdapter.setOnLoadMoreListener(this, dataBinding.recycler);
        audioSingerRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MusicSingerBean.ArtistsEntity bean = (MusicSingerBean.ArtistsEntity)baseQuickAdapter.getData().get(i);
                if(!UIUtils.isEmpty(bean)){
                    Bundle bundle = new Bundle();
                    bundle.putString("music_singer_head",bean.getImg1v1Url()+"");
                    bundle.putString("music_id",bean.getId()+"");
                    bundle.putString("music_author",bean.getName()+"");
                    ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_AUDIO_SINGER_SONG).with(bundle).navigation();
                }
            }
        });
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("歌手");

        dataBinding.refreshLayout.setColorSchemeResources(R.color.color_sys);
        dataBinding.refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                pageNumber = 0;
                mViewModel.getMusicSinger(type,area,singerName,pageSize,pageNumber,System.currentTimeMillis()+"",false);
            }
        });

        mViewModel.listData.observe(this,listBean->{
            dataBinding.refreshLayout.setRefreshing(false);
            if(!UIUtils.isEmpty(listBean)){
                if(!mViewModel.canRefresh) audioSingerRecycleAdapter.setEnableLoadMore(false);
                if(isRefresh){
                    audioSingerRecycleAdapter.setNewData(listBean);
                }else {
                    audioSingerRecycleAdapter.addData(listBean);
                    audioSingerRecycleAdapter.loadMoreComplete();
                }
            }else {
                Log.d("getMusicSinger", "error=null===");
                if(mViewModel.canRefresh){
                    audioSingerRecycleAdapter.setEmptyView(R.layout.empty_view, (ViewGroup) dataBinding.recycler.getParent());
                }else {
                    audioSingerRecycleAdapter.setEnableLoadMore(false);
                }
            }
        });
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });
        dataBinding.etSingerName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!UIUtils.isEmpty(s) && s.toString().trim().length()>0){
                    audioSingerRecycleAdapter.getData().clear();
                    audioSingerRecycleAdapter.notifyDataSetChanged();
                    pageNumber = 0;
                    singerName = s.toString().trim();
                    getData(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public void onLoadMoreRequested() {
        if(mViewModel.canRefresh) getData(false);

    }
}
